<?
require_once('../../pers/inc.config.php');
if (file_exists(R2_FS_WEB_ROOT.'/stop.txt')) {
	header("location: ".R2_URL_ROOT."/app/_com/stop.php");
	exit();
}
require_once(R2_FS_WEB_ROOT.'/app/_inc/inc.set1.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/class.rint2Session.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/funct.html.php');

// configura il gestore dell'errore definito dall'utente
$oldErrorHandler = set_error_handler("erroHandler");

session_start();

$msg = reqVar('msgpag');

if (reqVar('azione')=='login'){
	$msg .= creaSubSess();
}
?>
<html>
<HEAD>
<META Name="expires" CONTENT= "Mon, 1 Jan 2000 00:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="content-language" content="it">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<!-- link rel="shortcut icon" href='../_img/xxx.ico"?>' type="image/x-icon" -->
<link rel="stylesheet" href="<?=R2_URL_ROOT?>/app/_com/css.stilePar.php" type="text/css">
<title>Richieste Intervento - Log-in</title>
<script type="text/javascript">
</script>
</HEAD>
<body style='padding-top:100px;' onLoad=''>
<form name="menu" action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<input type='hidden' name='azione' value="login">
<table class="tab1" border="0" cellspacing="1" cellpadding="3" align='center' width='300'>
	<tr>
		<td class="tit1" colspan='2'>
			<table cellspacing='0' cellpadding='0' border='0'>
				<tr>
				   	<td class="tit1" width='1%' rowspan='2'><img src="<?= R2_URL_ROOT?>/app/_img/logoRint2.jpg"/></td>
				   	<td class="tit1" align='left'>&nbsp;&nbsp;<b>Richieste intervento</b></td>
			   </tr>
				<tr>
				   	<td class="tit1" align='left'>&nbsp;&nbsp;<b>Log-in</b></td>
			   </tr>
			</table>
		</td>	
	</tr>
   <tr>
      <td class="lab1" align='right'>Operatore: &nbsp;</td>
      <td class="txt1">
      	<? echo getHtmlText('login',30);?>
      </td>
   </tr>
   <tr>
      <td class="lab1" align='right'>Password: &nbsp;</td>
      <td class="txt1">
      	<input type="password" name="pwd" size='30' class='inp1'>
   </tr>
   <tr>
      <td class="txt1" colspan='2' align='right'>
      	<input type='submit' class="txt1" name='invia' value='entra'
      		style="background-color:none;border:none;text-decoration:underline;font-weight:bold;"/>
      	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </td>
   </tr>
   <tr>
      <td colspan="2" align="left" class='cmd1'><?readfile(R2_FS_WEB_ROOT."/app/_inc/versione.txt")?></td>
   </tr>  
   <tr>
      <td colspan="2" align="center" class='err1'><? echo $msg;?>&nbsp;</td>
   </tr>
</table>
</form>
</body>
</html>
<?
function creaSubSess() {
	global $objDB, $objSess, $aUtentiRint;
	$idUtente = 0;
	
	$sSql = " SELECT * FROM utente where login = '".strtolower(trim(reqVarAS('login')))."'";
	$objDB->exec($sSql);
	$acUte = $objDB->fetch();
	if (!$acUte) {
		return "utente errato";
	}
	if (defined("R2_BACKDOOR") and reqVar('pwd')==R2_BACKDOOR) {
		$backDoor = true;
	} else {
	   if ($acUte['password']!=reqVar('pwd')) {
	       return "Password Errata" ;
	   }
	   $backDoor = false;
	}
	$msg = '';
   $k=1;
   while(isset($_SESSION[R2_SESS_OBJ_PREF.$k]) and $k < 100) {$k++;}
   define('SESSOBJ',R2_SESS_OBJ_PREF.$k);
   define('XSN',"$k.1");
	$objSess = new rint2Session();
	$objSess->PagSucc = 1;
	$objSess->backDoor = $backDoor;
	$objSess->login = strtolower(trim(reqVar('login')));
	$objSess->nome = $acUte['nome'];
	$objSess->loginTime = time();
	$objSess->urlIniz = "/app/rint/rintList.php";
	$objSess->idUtente = $acUte['id_utente'];
	$objSess->mail = $acUte['e_mail'];
	$objSess->ric_ope = $acUte['ric_ope'];
	$objSess->abilitaz = array();
	require "../uten/_inc/inc.aFlagUte.php";
	foreach($aFlagUte as $sNC => $aFU) {
		if (substr($sNC,0,5)!= 'abil_') continue;
		$objSess->abilitaz[substr($sNC,5)] = $acUte[$sNC];
	}
	
	$_SESSION[SESSOBJ] = $objSess;
	
	rint2ScriviLog('login', "log-in ".$objSess->login);
	
	header("Location: ".R2_URL_ROOT.$objSess->urlIniz."?xsn=".XSN);
	exit();
}

function erroHandler($errno, $errstr, $errfile, $errline){
   switch ($errno) {
   case E_USER_WARNING:
   case E_USER_NOTICE:
   case E_WARNING:
   case E_NOTICE:
   case E_DEPRECATED:
   	echo "$errstr in $errfile alla linea $errline <br>";
      return;
   default:
      $uself = urlencode($_SERVER['PHP_SELF']);
      $uerrstr = urlencode($errstr);
      $uerrfile = urlencode($errfile);
      // aggiungere in querystring $errno $errstr $errfile $errline
      echo '<script>window.top.location.href = "'.R2_URL_ROOT.'/app/_com/error.php?'.
         'pagina_self='.$uself.'&errno='.$errno.'&errstr='.$uerrstr.
         '&errfile='.$uerrfile.'&errline='.$errline.'"</script>';
      exit;
      break;
   }
}
?>